package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallDailyFeedbackConfirmView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.DailyFeedBackStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallDailyFeedbackConfirmPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/6/9.
 */

public class EngineeringInstallCompletionFeedBackMapForManagerActivity extends Activity implements View.OnClickListener, IEngineeringInstallDailyFeedbackConfirmView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_project_name, tv_engineering_name;
    private View rl_confirm_person, rl_confirm_time;
    private TextView edt_feedback_people_colon, edt_feed_back_time, edt_confirm_person, edt_confirm_time;
    private GridView gridview_completion, gridview_texie_photo, gridview__other_photo;
    private TextView btn_confirm;
    private GridAdapter gridview_completiondapter, gridview_texie_photoadapter, gridview__other_photoadapter;

    private List<String> serverimgUrlListList = new ArrayList<>();
    private List<String> serverortherimgUrlListList = new ArrayList<>();
    private List<String> servertexieimgUrlListList = new ArrayList<>();

    private EngineeringDailyFeedBackBean engineeringDailyFeedBackBean;
    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;

    private EngineeringInstallDailyFeedbackConfirmPresenter engineeringInstallDailyFeedbackConfirmPresenter;
    private String dailyFeedbackId, orderId;
    private CommonEnum.EngineeringORderCategory type;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        setContentView(R.layout.layout_engineeringinstall_completionfeedback_map_for_surper);

        engineeringInstallDailyFeedbackConfirmPresenter = new EngineeringInstallDailyFeedbackConfirmPresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            String tag = mIntent.getStringExtra(ConstantUtil.KEY_1);
            if (!TextUtils.isEmpty(tag)) {
                if (tag.equals("EngineeringInstallFeedBackForWaitStateActivity")) {
                    engineeringInstallationOrderBean = (EngineeringInstallationOrderBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
                    engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) mIntent.getSerializableExtra("feedbackbean");

                    if (null != engineeringInstallationOrderBean)
                        orderId = engineeringInstallationOrderBean.getId();

                    if (null != engineeringDailyFeedBackBean)
                        dailyFeedbackId = engineeringDailyFeedBackBean.getId();

                    type = CommonEnum.EngineeringORderCategory.INSTALL;

                } else if (tag.equals("EngineeringInstallStatusTableForManagerActivity")) {
                    DailyFeedBackStatuBean dailyFeedBackStatuBean = (DailyFeedBackStatuBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
                    if (null != dailyFeedBackStatuBean) {
                        dailyFeedbackId = dailyFeedBackStatuBean.getDailyFeedbackId();
                    }

                    EngineeringInstallStatuBean engineeringInstallStatuBean = (EngineeringInstallStatuBean) mIntent.getSerializableExtra("EngineeringInstallStatuBean");
                    if (null != engineeringInstallStatuBean) {
                        orderId = engineeringInstallStatuBean.getOrderId();
                    }

                    type = CommonEnum.EngineeringORderCategory.INSTALL;

                }
            }

//            engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
//            engineeringInstallationOrderBean = (EngineeringInstallationOrderBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

        }

        initData();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);

        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);

        rl_confirm_person = findViewById(R.id.rl_confirm_person);
        rl_confirm_time = findViewById(R.id.rl_confirm_time);

        gridview_completion = findViewById(R.id.gridview_completion);
        gridview_texie_photo = findViewById(R.id.gridview_texie_photo);
        gridview__other_photo = findViewById(R.id.gridview__other_photo);

        gridview_completion.setNumColumns(3);
        gridview_texie_photo.setNumColumns(3);
        gridview__other_photo.setNumColumns(3);

        gridview_completiondapter = new GridAdapter(this);
        gridview_texie_photoadapter = new GridAdapter(this);
        gridview__other_photoadapter = new GridAdapter(this);

        gridview_completiondapter.setData(serverimgUrlListList);
        gridview_texie_photoadapter.setData(servertexieimgUrlListList);
        gridview__other_photoadapter.setData(serverortherimgUrlListList);

        gridview_completion.setAdapter(gridview_completiondapter);
        gridview_texie_photo.setAdapter(gridview_texie_photoadapter);
        gridview__other_photo.setAdapter(gridview__other_photoadapter);


        edt_feedback_people_colon = findViewById(R.id.edt_feedback_people_colon);
        edt_feed_back_time = findViewById(R.id.edt_feed_back_time);
        edt_confirm_person = findViewById(R.id.edt_confirm_person);

        edt_confirm_time = findViewById(R.id.edt_confirm_time);

        btn_confirm = findViewById(R.id.btn_confirm);


        tv_title.setText("完工单");

        rl_back.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);


    }

    private void initData() {

        if (null != engineeringInstallationOrderBean) {
            tv_project_name.setText(engineeringInstallationOrderBean.getProjectName());
            tv_engineering_name.setText(engineeringInstallationOrderBean.getEngineeringName());

            List<String> imgUrlList = engineeringInstallationOrderBean.getCompleteImageList();
            if (null != imgUrlList) {
                serverimgUrlListList.clear();
                serverimgUrlListList.addAll(imgUrlList);

                gridview_completiondapter.setData(serverimgUrlListList);
                gridview_completiondapter.notifyDataSetChanged();
            }

            List<String> otherImgUrlList = engineeringInstallationOrderBean.getCompleteOtherImageList();
            if (null != otherImgUrlList) {
                serverortherimgUrlListList.clear();
                serverortherimgUrlListList.addAll(otherImgUrlList);

                gridview__other_photoadapter.setData(serverortherimgUrlListList);
                gridview__other_photoadapter.notifyDataSetChanged();
            }


            List<String> completeCloseUpImageList = engineeringInstallationOrderBean.getCompleteCloseUpImageList();
            if (null != completeCloseUpImageList) {
                serverimgUrlListList.clear();
                serverimgUrlListList.addAll(completeCloseUpImageList);

                gridview_completiondapter.setData(completeCloseUpImageList);
                gridview_completiondapter.notifyDataSetChanged();
            }

        }


        if (null != engineeringDailyFeedBackBean) {

            edt_feedback_people_colon.setText(engineeringDailyFeedBackBean.getAccountId());
            edt_feed_back_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getFeedbackDate()));

            String handlerAccountId = engineeringDailyFeedBackBean.getHandlerAccountId();
            if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
                rl_confirm_person.setVisibility(View.VISIBLE);
                rl_confirm_time.setVisibility(View.VISIBLE);
                btn_confirm.setVisibility(View.GONE);

                edt_confirm_person.setText(engineeringDailyFeedBackBean.getHandlerAccountId());
                edt_confirm_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getHandleDate()));

            } else {
                rl_confirm_person.setVisibility(View.GONE);
                rl_confirm_time.setVisibility(View.GONE);
                btn_confirm.setVisibility(View.VISIBLE);
            }


//
//            CommonEnum.UnusualStatus unusualStatus = engineeringDailyFeedBackBean.getUnusualStatus();
//            if (null != unusualStatus) {
//                if (unusualStatus == CommonEnum.UnusualStatus.WAIT_CONFIRM) {
//                    btn_confirm.setVisibility(View.VISIBLE);
//
//                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY) {
//                    btn_confirm.setVisibility(View.GONE);
//                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM) {
//
//                } else if (unusualStatus == CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH) {
//
//                }
//            }
        }

    }

    private void feedbackConfirm() {
        engineeringInstallDailyFeedbackConfirmPresenter.engineeringIstallDailyFeedbackConfirm();
    }


    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;

            case R.id.btn_confirm:
                feedbackConfirm();
                break;


        }
    }

    @Override
    public String getcontent() {
        return null;
    }

    @Override
    public String getDailyFeedbackId() {
        if (null != engineeringDailyFeedBackBean)
            return engineeringDailyFeedBackBean.getId();
        return null;
    }

    @Override
    public String getOrderId() {
        if (null != engineeringInstallationOrderBean)
            return engineeringInstallationOrderBean.getId();

        return null;
    }

    @Override
    public CommonEnum.EngineeringORderCategory getType() {

        return CommonEnum.EngineeringORderCategory.INSTALL;
    }

    @Override
    public void feeedbackSuccessed() {
        Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
